package rcs34.android.AndroidRC;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.logging.Logger;

import org.json.JSONException;
import org.json.JSONObject;

import rcs34.android.AndroidRC.Receivers.ARCBroadcastReceiver;
import android.app.ActivityManager;
import android.app.Application;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Handler;
import android.os.Messenger;


public class GApp extends Application {
  public static String LOGTAG = GApp.class.getPackage().getName();
  public static String KEY_INFO_MODEL = "model";
  public static Context context;
  public static Handler handler;
  public static Resources resources;
  public static JSONObject infoJSON = new JSONObject();
  public static Messenger serviceStatusMessenger;
  public static HashMap<String, CacheData> staticCache = new HashMap<String, CacheData>();
  public static int socketsOpened = 0;
  public static Logger logger;
  public static ActivityManager activityManager;
  public static ContentResolver contentResolver;
  public static IPRegister ipRegister = new IPRegister();
  public static Runtime runtime = Runtime.getRuntime();
  private byte[] workBuffer;
  private AssetManager assetManager;
  public static ATimer inactivity = new ATimer();
  
  public GApp() {
    context = this;
  }
  
  @Override
  public void onCreate() {
    activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    contentResolver = context.getContentResolver();
    handler = new Handler();
    resources = getResources();
    try {
      infoJSON.put(KEY_INFO_MODEL,android.os.Build.MANUFACTURER +" "+ android.os.Build.MODEL);
    } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    Intent i = new Intent(context, ServiceSender.class);
    i.putExtra(ServiceSender.KEY_CMD, ServiceSender.CHANGE_STORAGE);
    context.startService(i);
    Intent batteryStatus = context.registerReceiver(null, new IntentFilter(
        Intent.ACTION_BATTERY_CHANGED));
    ARCBroadcastReceiver.fireBattIntent(context, batteryStatus);
    // System.out.println("Caching");
    workBuffer = new byte[1024 * 15];
    assetManager = context.getAssets();
    cacheIt("favicon.ico", HttpMessage.MIME_ICON, null);
    cacheIt("index.html", HttpMessage.MIME_HTML, null);
    cacheIt("popup.js", HttpMessage.MIME_JS, null);
    cacheIt("info.js", HttpMessage.MIME_JS, null);
    cacheIt("fm.js", HttpMessage.MIME_JS, null);
    cacheIt("fm_mimes.js", HttpMessage.MIME_JS, null);
    cacheIt("logcat.js", HttpMessage.MIME_JS, null);
    cacheIt("cmdline.js", HttpMessage.MIME_JS, null);
    cacheIt("contacts.js", HttpMessage.MIME_JS, null);
    cacheIt("contacts_details.js", HttpMessage.MIME_JS, null);
    cacheIt("main.js", HttpMessage.MIME_JS, null);
    cacheIt("style.css", HttpMessage.MIME_CSS, null);
    cacheIt("fm_uploader.js", HttpMessage.MIME_JS, null);
    cacheIt("fm_deleter.js", HttpMessage.MIME_JS, null);
    cacheIt("tasks.js", HttpMessage.MIME_JS, null);
    cacheIt("loading.gif", HttpMessage.MIME_GIF, null);
    cacheIt("favicon.png", HttpMessage.MIME_PNG, null);
    cacheIt("file.png", HttpMessage.MIME_PNG, null);
    cacheIt("folder.png", HttpMessage.MIME_PNG, null);
    cacheIt("ic_audio.png", HttpMessage.MIME_PNG, null);
    cacheIt("bg_wtitle.png", HttpMessage.MIME_PNG, null);
    cacheIt("bg_toolbar.png", HttpMessage.MIME_PNG, null);
    cacheIt("bg_button.png", HttpMessage.MIME_PNG, null);
    cacheIt("none.png", HttpMessage.MIME_PNG, null);
    cacheIt("encoding.js", HttpMessage.MIME_JS, null);
    cacheIt("login.js", HttpMessage.MIME_JS, null);
    cacheIt(this.getString(R.string.textsjs), HttpMessage.MIME_JS, "texts.js");
    workBuffer = null;
    assetManager = null;
  }
  
  private void cacheIt(String _file, String _mime, String _saveAs) {
    try {
      int length = 0;
      String saveAs = _file;
      if (_saveAs != null) {
        saveAs = _saveAs;
      }
      InputStream is = assetManager.open("www" + File.separator + _file);
      length = is.read(workBuffer);
      byte[] cacheData = new byte[length];
      System.arraycopy(workBuffer, 0, cacheData, 0, length);
      CacheData cache = new CacheData(cacheData, _mime, 20);
      staticCache.put("/" + saveAs, cache);
      // System.out.println(new String(cache.data));
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}
